* Load dataset
use "replication data - main.dta", clear

keep if year >1990	

gen Islamist_terror = ln_Islamkill1yr
gen nonIslamist_terror = ln_nonIslamkill1yr 

local main 			"(c.Islamist_terror c.nonIslamist_terror)##i.islamic_opposition"
local controls 		"c.problems muslim00 c.polity2 polity2_change ln_mortality firstmpany transitional_election postconflict pre_election_violence ln_oil lln_total_trade col_any postcoup ln_gdptotal legelec"

************
* FIGURE 1 *
************

gen str pipe="|"
gen where=-1		// "-1", because I don't want my CI and rugplot overlapping

reg acceptable `main' `controls', cl(ccode)

margins, dydx(islamic_opposition) at(nonIslamist_terror=0 Islamist_terror=(0(0.25)5.25))

mat r=r(table)'
mat r=r`j'[1..., 1..6]
svmat r, names(coef)
mat a=r(at)
svmat a, names(at)
replace at1=at1[_n-22] in 23/44
replace coef1=. in 1/22


* draw Figure 1
twoway (hist Islamist_terror if e(sample) & Islamist_terror>0, width(0.2) percent yaxis(2) color(gs12)) ///
(line coef1 coef5 coef6 at1, clpattern(solid solid solid) yaxis(1) lcolor(black black black)), 		///
ysca(noline alt) ysca(off r(0(5)100) axis(2)) legend(off) scheme(plotplain)						///
yline(0, lpattern(dash)) xtitle("log of Islamist Terrorist Killings")
graph export figure1.pdf


*************
* FIGURE A1 *
*************


logit acceptable `main' `controls', cl(ccode)

margins, dydx(islamic_opposition) at(nonIslamist_terror=0 Islamist_terror=(0(0.25)5.25))


mat r=r(table)'
mat r=r`j'[1..., 1..6]
svmat r, names(coef_lgt)
mat a=r(at)
svmat a, names(at_lgt)
replace at_lgt1=at_lgt1[_n-22] in 23/44
replace coef_lgt1=. in 1/22


* draw Figure A1
twoway (hist Islamist_terror if e(sample) & Islamist_terror>0, width(0.2) percent yaxis(2) color(gs12)) ///
(line coef_lgt1 coef_lgt5 coef_lgt6 at_lgt1, clpattern(solid solid solid) yaxis(1) lcolor(black black black)), 		///
ysca(noline alt) ysca(off r(0(5)100) axis(2)) legend(off) scheme(plotplain)		///
yline(0, lpattern(dash)) xtitle("log of Islamist Terrorist Killings")
graph export figureA1.pdf


*****************
* FIGURES A2-A5 *
*****************
	
* run the original model in order to get "e(sample)"
reg acceptable `main' `controls', cl(ccode)

gen ccode_sample = ccode
replace ccode_sample =. if e(sample) ==0

gen newid_sample = newid
replace newid_sample =. if e(sample) ==0

* We will save the coef's in these new variables
gen coefs_newid = .
gen coefs_ccode = .

gen se_newid = .
gen se_ccode = .

gen t_newid = .
gen t_ccode = .

local i = 1


* First, leave out elections one-by-one 
levelsof newid_sample
levelsof newid_sample, local(levels)  

 foreach l of local levels {
	display `l'

reg acceptable `main' `controls' if newid !=`l', cl(ccode)
	
    mat E = e(b)
    replace coefs_newid = E[1,6] in `i'
	
	mat V = e(V)
	replace se_newid = sqrt(V[6,6]) in `i'

    replace t_newid  = coefs_newid / se_newid
	
	local `i++'	
	
}


* Second, leave out countries one-by-one
local i = 1

levelsof ccode_sample
levelsof ccode_sample, local(levels) 

 foreach l of local levels {
	display `l'

reg acceptable `main' `controls' if ccode !=`l', cl(ccode)
	
    mat E = e(b)
    replace coefs_ccode = E[1,6] in `i'
	
	mat V = e(V)
	replace se_ccode = sqrt(V[6,6]) in `i'

    replace t_ccode  = coefs_ccode / se_ccode
	
    local `i++'	
	
}


**************
* HISTOGRAMS *
**************

* FIGURE A2
hist coefs_newid, scheme(plotplain) addplot(pci 0 0.182 150 0.182, lpattern(dash) lcolor(black)) legend(off) xtitle("Coef's for (Islamist Terrorism * Islamic Opposition)") xline(0.182)
graph export figureA2.pdf

* FIGURE A3
hist coefs_ccode, scheme(plotplain) addplot(pci 0 0.182 150 0.182, lpattern(dash) lcolor(black)) legend(off) xtitle("Coef's for (Islamist Terrorism * Islamic Opposition)") xline(0.182)
graph export figureA3.pdf

* FIGURE A4
hist t_newid, scheme(plotplain) legend(off) xtitle("t-values for (Islamist Terrorism * Islamic Opposition)")
graph export figureA4.pdf

* FIGURE A5
hist t_ccode, scheme(plotplain) legend(off) xtitle("t-values for (Islamist Terrorism * Islamic Opposition)") 
graph export figureA5.pdf
